package com.jd.jobkeeper.server.web.controller;


import com.jd.jobkeeper.server.data.entity.JobInfo;
import com.jd.jobkeeper.server.scheduler.ServerNode;
import com.jd.jobkeeper.server.service.JobInfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/job")
public class JobController {

    final JobInfoService jobInfoService;
    final ServerNode serverNode;

    public JobController(JobInfoService jobInfoService, ServerNode serverNode) {
        this.jobInfoService = jobInfoService;
        this.serverNode = serverNode;
    }

    @PostMapping
    public JobInfo addJob(JobInfo jobInfo){
        return this.jobInfoService.add(jobInfo);
    }

    @PostMapping("/stop")
    public JobInfo stopTask(@RequestParam("jobId") long jobId){
        JobInfo jobInfo = jobInfoService.getByJobId(jobId);
        serverNode.killTask(jobInfo).block();
        return jobInfo;
    }

}
